package com.example.myapplication.Media;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.example.myapplication.R;

public class LocalPicActivity extends AppCompatActivity {
  private static final int REQUEST_IMAGE_CAPTURE = 1;
  private static final int REQUEST_SELECT_IMAGE = 2;
  ImageView imageView;
  private ActivityResultLauncher<Intent> captureLauncher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
      result -> {
        if (result.getResultCode() == Activity.RESULT_OK) {
          Intent data = result.getData();
          Bitmap photoBitMap = (Bitmap) data.getExtras().get("data");
          imageView.setImageBitmap(photoBitMap);

        }
      });
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.media_picture_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("图片浏览");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    SetView();
  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        GoBack();
    }
    return true;
  }
  private void GoBack() {
    finish();
  }
  private void SetView() {
    imageView = (ImageView) findViewById(R.id.ImgView);
    Button cameraButton = (Button) findViewById(R.id.camera_button);
    Button localButton = (Button) findViewById(R.id.local_button);
    cameraButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        TakePhoto();
      }
    });
    localButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        SelectLocalPicture();
      }
    });
  }

  private void SelectLocalPicture() {
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, REQUEST_SELECT_IMAGE);
  }

  private void TakePhoto() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
      captureLauncher.launch(takePictureIntent);
    }
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_SELECT_IMAGE && resultCode == RESULT_OK && data != null) {
      Uri selectedImageUri = data.getData();
      try {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
        imageView.setImageBitmap(bitmap);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
  }
}
